﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuxiWTO.Mapping;
using WutiWTO.Common;
using System.Data.Linq;

namespace WuxiWTO.Web.backend
{
    public partial class friendlinkEdit : BasePage
    {
        WTODataContext contex = new WTODataContext();

        int cid = 0;
        /// <summary>
        /// 更新模式
        /// </summary>
        protected bool UpdateMode
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    cid = int.Parse(Request.QueryString["id"]);
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //BindGrid();
                if (UpdateMode)
                {
                    GetInfo();
                }
            }
        }

        private void GetInfo()
        {
            friend_link cat = contex.friend_links.SingleOrDefault(p => p.link_id == cid);
            if (cat != null)
            {
                this.txtName.Text = cat.link_name;
                this.txtURL.Text = cat.url;
                this.txtOrder.Text = cat.order_id.ToString();
                if (!string.IsNullOrEmpty(cat.link_image))
                {
                    this.litPic.Text = string.Format("<img alt='' src=\"{0}\"/>", "../" + cat.link_image);
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "fa", "alert('参数传递失败');", true);
            }
        }


        protected void btnOK_Click(object sender, EventArgs e)
        {

            int order_id = 0;
            int.TryParse(this.txtOrder.Text.Trim(), out order_id);
            friend_link link;
            if (UpdateMode)
            {
                link = contex.friend_links.SingleOrDefault(p => p.link_id == cid);
                if (link == null)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "fa", "alert('参数传递失败');", true);
                    return;
                }
            }
            else
                link = new friend_link();
            link.link_name = this.txtName.Text.Trim();
            link.order_id = order_id;
            string strURL = this.txtURL.Text.Trim();
            if (!strURL.Contains("://"))
            {
                strURL = string.Concat("http://", strURL);
            }
            link.url = strURL;
            link.update_date = DateTime.Now;
            if (!UpdateMode)
            {
                link.created_date = DateTime.Now;
                contex.friend_links.InsertOnSubmit(link);
            }

            if (fileLink.HasFile)
            {
                if (".jpg.jpeg.gif.png".Contains(System.IO.Path.GetExtension(fileLink.FileName).ToLower()))
                {
                    string filePath = Server.MapPath(@"~/uploads/links/");
                    string fileName = fileLink.FileName;
                    string savePath = filePath + fileName;
                    if (!System.IO.Directory.Exists(filePath))
                    {
                        System.IO.DirectoryInfo path = System.IO.Directory.CreateDirectory(filePath);
                    }
                    fileLink.SaveAs(savePath);

                    //Utility.MakeThumbnail(savePath, filePath + "thumb_" + fileName, Utility.ThumbType.HW, 200, 30);
                    link.link_image = "uploads/links/"  + fileName;
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "fa1", "alert('上传图片格式错误,请上传后缀名为:.jpg/.jpeg/.gif/.png格式的图片');", true);
                    return;
                }
            }
            
            contex.SubmitChanges();
            Response.Redirect("friendLinkList.aspx");
        }
    }
}